﻿Ext.namespace('Ext.ux.grid');

Ext.ux.grid.RemoteGridPanel = function(config) {
	Ext.apply(this,config);
	
	if (!config.cm) {
		config.cm = new Ext.grid.ColumnModel([{
			header : ""
		}]);
	}

	Ext.applyIf(this, config);

	if (!this.store) {
		this.store = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : this.url
			}),
			baseParams : this.baseParams,
			reader : new Ext.data.JsonReader({
				listeners : {
					'metachange' : this.onMetaChange.createDelegate(this)
				}
			})
		});
	}

	if (this.store) {
		this.store.on("metachange", this.onMetaChange, this);
	}

	this.store.load({
		params : this.params
	});
	
	if (this.bbar){
		var bar = this.bbar;
		bar.store = this.store;
		delete this.bbar;
		this.bbar = new Ext.PagingToolbar(bar); 
	}

	Ext.ux.grid.RemoteGridPanel.superclass.constructor.call(this);
	
	
};
Ext.extend(Ext.ux.grid.RemoteGridPanel, Ext.grid.GridPanel, {
	cellRenderers : {
		'cnMoney' : Ext.util.Format.cnMoney,
		'decimal' : Ext.util.Format.decimal,
		'cnUpperMoney' : Ext.util.Format.cnUpperMoney,
		'date' : Ext.util.Format.date,
		'datetime' : Ext.util.Format.datetime
	},

	addRenderer : function(name, fn) {
		this.cellRenderers[name] = fn;
	},

	onMetaChange : function(store, meta) {
		var field;
		var config = [];
		var autoExpand = false;
		for (var i = 0; i < meta.columns.length; i++) {
			// loop for every dataIndex, only add fields with a header property
			field = meta.columns[i];
			if (field.header !== undefined) {
				// field.dataIndex = field.name;

				// search for cell render functions by [field.renderer] or
				// _[field.name]
				if (this.cellRenderers[field.renderer]) {
					field.renderer = this.cellRenderers[field.renderer];
				} else if (this.cellRenderers["_" + field.name]) {
					field.renderer = this.cellRenderers["_" + field.name];
				}

				// Auto assign an id if none given.
				if (!field.id) {
					field.id = 'c' + i;
				}

				// if width is auto, set autoExpand variabel (should only be set
				// after reconfigure for some reason)
				if (field.width == "auto") {
					autoExpand = field.id;
					field.width = 100;
				}

				// add to the config (field.name is replaced by dataIndex)
				delete field.name;
				config[config.length] = field;
			}
		}

		// Create the new cm, and update the gridview.
		var cm = new Ext.grid.ColumnModel(config);
		this.reconfigure(this.store, cm);

		this.autoExpandColumn = autoExpand;
	}
});


Ext.ux.grid.SqlGridPanel = function(config){
	Ext.apply(this,config);
	config.url = ExtJ.getActionPath('com.suyuan.framework.widgets.grid.SqlGridBuilderAction');
	config.baseParams =  {gridBuilder:config.gridBuilder};
	if (!config.params){
		config.params = {start:0,limit:15};
	}
	Ext.apply( config.params,{gridBuilder:config.gridBuilder});
	Ext.ux.grid.SqlGridPanel.superclass.constructor.call(this,config);
}

Ext.extend(Ext.ux.grid.SqlGridPanel,Ext.ux.grid.RemoteGridPanel, {
});


Ext.apply(Ext.util.Format, {
	cnMoney : function(v) {
		v = (Math.round((v - 0) * 100)) / 100;
		v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
		v = String(v);
		var ps = v.split('.');
		var whole = ps[0];
		var sub = ps[1] ? '.' + ps[1] : '.00';
		var r = /(\d+)(\d{3})/;
		while (r.test(whole)) {
			whole = whole.replace(r, '$1' + ',' + '$2');
		}
		v = whole + sub;
		if (v.charAt(0) == '-') {
			return '-￥' + v.substr(1);
		}
		return "￥" + v;
	},
	decimal : function(v) {
		v = (Math.round((v - 0) * 100)) / 100;
		v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
		v = String(v);
		var ps = v.split('.');
		var whole = ps[0];
		var sub = ps[1] ? '.' + ps[1] : '.00';
		var r = /(\d+)(\d{3})/;
		while (r.test(whole)) {
			whole = whole.replace(r, '$1' + ',' + '$2');
		}
		v = whole + sub;
		if (v.charAt(0) == '-') {
			return '-' + v.substr(1);
		}
		return "" + v;
	},

	cnUpperMoney : function(num) {
		if (isNaN(num) || num > Math.pow(10, 14))
			return ""
		var symbol;// 1 ： 正 0 : 负
		if (num >= 0) {
			symbol = "";
		} else {
			symbol = "负";
		}

		num = Math.abs(num);
		var cn = "零壹贰叁肆伍陆柒捌玖"
		var unit = new Array("拾佰仟", "分角")
		var unit1 = new Array("万亿", "")
		var numArray = num.toString().split(".")
		var start = new Array(numArray[0].length - 1, 2)

		function toChinese(num, index) {
			var num = num.replace(/\d/g, function($1) {
				return cn.charAt($1) + unit[index].charAt(start-- % 4 ? start % 4 : -1);
			});
			return num
		}

		for (var i = 0; i < numArray.length; i++) {
			var tmp = "";
			var flag = true;
			for (var j = 0; j * 4 < numArray[i].length; j++) {
				var strIndex = numArray[i].length - (j + 1) * 4;
				var str = numArray[i].substring(strIndex, strIndex + 4);
				var start = i ? 2 : str.length - 1;

				var tmp1 = toChinese(str, i);

				tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "");
				// tmp1 = tmp1.replace(/^壹拾/, "拾");
				if (str != "0000") {
					var tempUnit = "";
					if (j < 3) {
						tempUnit = unit1[i].charAt(j - 1);
					} else {
						tempUnit = flag ? "万亿" : "万";
					}
					tmp = (tmp1 + tempUnit) + tmp;
					flag = false;
				} else {
					if (j === 2) {
						flag = true;
					}
				}
			}
			numArray[i] = tmp;
		}

		numArray[1] = numArray[1] ? numArray[1] : "";
		numArray[0] = numArray[0] ? numArray[0] + "圆" : numArray[0], numArray[1] = numArray[1].replace(/^零+/, "");
		numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1] + "整"
		return symbol + numArray[0] + numArray[1];
	},
	date : function(v) {
		if (!v) {
			return "";
		}
		if (!(v instanceof Date)) {
			v = new Date(Date.parseDate(v, 'Y-m-d'));
		}
		return v.dateFormat("Y-m-d");
	},
	datetime : function(v) {
		if (!v) {
			return "";
		}
		if (!(v instanceof Date)) {
			v = new Date(Date.parseDate(v, 'Y-m-d h:i:s'));
		}
		return v.dateFormat("Y-m-d h:i:s");
	}

});


